﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace LangResEditor
{
    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            InitializeComponent();
        }
        Hashtable m_hashData = new Hashtable();
        private void button_Open_Click(object sender, EventArgs e)
        {

            if (openFileDialog_Open.ShowDialog() == DialogResult.OK)
            {
                TextReader tr = new StreamReader(openFileDialog_Open.FileName, System.Text.Encoding.Default);
                try
                {
                    string strLine = tr.ReadLine();
                    while (strLine != null && strLine != "")
                    {
                        string[] strary = strLine.Split('\t');
                        if (!m_hashData.Contains(strary[0]))
                        {
                            m_hashData[strary[0]] = strary[1];
                        }
                        strLine = tr.ReadLine();
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    tr.Close();

             
                }
                updateGrid();
            }

        }

        private void updateGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");

            foreach (string strKey in m_hashData.Keys)
            {
                string[] strary = new string[2];
                strary[0] = strKey;
                strary[1] = (string)m_hashData[strKey];
                dt.Rows.Add(strary);
            }

            this.dataGridView_Data.DataSource = dt;
        }

        private void dataGridView_Data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_Data.SelectedRows.Count > 0)
            {
                DataGridViewRow dgvr = dataGridView_Data.SelectedRows[0];
                this.textBox_Name.Text = (string)dgvr.Cells[0].Value;
                this.textBox_Value.Text = (string)dgvr.Cells[1].Value;
            }
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            if (!m_hashData.Contains(this.textBox_Name.Text))
            {
                m_hashData[this.textBox_Name.Text] = this.textBox_Value.Text;
                updateGrid();
            }
        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            if (m_hashData.Contains(this.textBox_Name.Text))
            {
                m_hashData.Remove(this.textBox_Name.Text);
                updateGrid();
            }
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            m_hashData[this.textBox_Name.Text] = this.textBox_Value.Text;
            updateGrid();
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            if (saveFileDialog_Save.ShowDialog() == DialogResult.OK)
            {
                TextWriter tw = new StreamWriter(saveFileDialog_Save.FileName,false, System.Text.Encoding.Default);
                foreach (string strKey in m_hashData.Keys)
                {
                    tw.WriteLine(strKey + "\t" + (string)m_hashData[strKey]);
                }

                tw.Close();
            }
        }

        private void button_Query_Click(object sender, EventArgs e)
        {
            if (m_hashData.Contains(this.textBox_Name.Text))
            {
                this.textBox_Value.Text = (string)m_hashData[this.textBox_Name.Text];
            }
            else
            {
                this.textBox_Value.Text = "";
            }

        }
    }
}